// SPDX-FileCopyrightText: The openTCS Authors
// SPDX-License-Identifier: MIT
package org.opentcs.guing.common.components.properties.panel;

import static java.util.Objects.requireNonNull;

import org.opentcs.guing.base.model.AcceptableOrderTypeModel;
import org.opentcs.guing.common.components.dialogs.DialogContent;
import org.opentcs.guing.common.transport.OrderTypeSuggestionsPool;
import org.opentcs.guing.common.util.I18nPlantOverview;
import org.opentcs.thirdparty.guing.common.jhotdraw.util.ResourceBundleUtil;

/**
 * User interface to create a single order type.
 */
public class OrderTypePanel
    extends
      DialogContent {

  private final OrderTypeSuggestionsPool orderTypeSuggestionsPool;
  private final AcceptableOrderTypeModel orderTypeTemplate;
  private final ResourceBundleUtil bundle
      = ResourceBundleUtil.getBundle(I18nPlantOverview.PROPERTIES_PATH);

  /**
   * Creates a new instance.
   *
   * @param orderTypeTemplate The order type that should be used as a template (i.e. to fill this
   * panel's components).
   * @param orderTypeSuggestionsPool The pool of order types to suggest.
   */
  @SuppressWarnings("this-escape")
  public OrderTypePanel(
      AcceptableOrderTypeModel orderTypeTemplate,
      OrderTypeSuggestionsPool orderTypeSuggestionsPool
  ) {
    this.orderTypeTemplate = requireNonNull(orderTypeTemplate, "orderTypeTemplate");
    this.orderTypeSuggestionsPool
        = requireNonNull(orderTypeSuggestionsPool, "orderTypeSuggestionsPool");

    initComponents();
    initNameComboBox();
    fillComponents();
  }

  @Override
  public String getDialogTitle() {
    return bundle.getString("orderTypePanel.title");
  }

  @Override
  public void initFields() {
  }

  @Override
  public void update() {
  }

  /**
   * Returns an {@link AcceptableOrderTypeModel} instance initialized with the data from this panel.
   *
   * @return An {@link AcceptableOrderTypeModel} instance.
   */
  public AcceptableOrderTypeModel getAcceptableOrderTypeModel() {
    return new AcceptableOrderTypeModel(
        nameComboBox.getSelectedItem().toString(),
        (int) prioritySpinner.getValue()
    );
  }

  private void initNameComboBox() {
    nameComboBox.removeAllItems();
    for (String suggestion : orderTypeSuggestionsPool.getTypeSuggestions()) {
      nameComboBox.addItem(suggestion);
    }
  }

  private void fillComponents() {
    nameComboBox.setSelectedItem(orderTypeTemplate.getName());
    prioritySpinner.setValue(orderTypeTemplate.getPriority());
  }

  // FORMATTER:OFF
  // CHECKSTYLE:OFF
  /**
   * This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    nameLabel = new javax.swing.JLabel();
    nameComboBox = new javax.swing.JComboBox<>();
    priorityLabel = new javax.swing.JLabel();
    prioritySpinner = new javax.swing.JSpinner();

    setLayout(new java.awt.GridBagLayout());

    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("i18n/org/opentcs/plantoverview/panels/propertyEditing"); // NOI18N
    nameLabel.setText(bundle.getString("orderTypePanel.label_name.text")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    add(nameLabel, gridBagConstraints);

    nameComboBox.setEditable(true);
    nameComboBox.setPreferredSize(new java.awt.Dimension(150, 22));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 3, 3, 0);
    add(nameComboBox, gridBagConstraints);

    priorityLabel.setText(bundle.getString("orderTypePanel.label_priority.text")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    add(priorityLabel, gridBagConstraints);

    prioritySpinner.setModel(new javax.swing.SpinnerNumberModel());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
    add(prioritySpinner, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JComboBox<String> nameComboBox;
  private javax.swing.JLabel nameLabel;
  private javax.swing.JLabel priorityLabel;
  private javax.swing.JSpinner prioritySpinner;
  // End of variables declaration//GEN-END:variables
  // CHECKSTYLE:ON
  // FORMATTER:ON
}
